/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import pako from "./pako.js"
let levelData = { level: 3 }
let ungzipTestData = { gzip: true, level: 9 }

const data1 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])
let array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])
let gzip = new pako.gzip(array, ungzipTestData);
let testNumber=200;

function test1() {
    let arr = new Array(10)
    let len = arr.length
    let start = Date.now()
    for (let i = 0; i < testNumber; i++) {
        arr[i % len] = new pako.Deflate(levelData)
    }
    let end = Date.now()
    print("pako_Deflate: " + (end - start) + " ms")
    for (let i = 0; i < 10; i++) {
        if (arr[i] == null) {
            print("Deflate fail")
        }
    }
}
function test2() {
    let arr = new Array(10)
    let len = arr.length
    let start = Date.now()
    for (let i = 0; i < testNumber; i++) {
        arr[i % len] = pako.deflate(data1)
    }
    let end = Date.now()
    print("pako_deflate: " + (end - start) + " ms")
    for (let i = 0; i < 10; i++) {
        if (arr[i] == null) {
            print("deflate fail")
        }
    }
}
function test3() {
    let arr = new Array(10)
    let len = arr.length
    let start = Date.now()
    for (let i = 0; i < testNumber; i++) {
        arr[i % len] = pako.gzip(array, ungzipTestData)
    }
    let end = Date.now()
    print("pako_gzip: " + (end - start) + " ms")
    for (let i = 0; i < 10; i++) {
        if (arr[i] == null) {
            print("gzip fail")
        }
    }
}
function test4() {
    let arr = new Array(10)
    let len = arr.length
    let start = Date.now()
    for (let i = 0; i < testNumber; i++) {
        arr[i % len] = pako.ungzip(gzip, ungzipTestData)
    }
    let end = Date.now()
    print("pako_ungzip: " + (end - start) + " ms")
    for (let i = 0; i < 10; i++) {
        if (arr[i] == null) {
            print("ungzip fail")
        }
    }
}
function test5() {
    let arr = new Array(10)
    let len = arr.length
    let start = Date.now()
    for (let i = 0; i < 2000; i++) {
        arr[i % len] = pako.Inflate(levelData)
    }
    let end = Date.now()
    print("pako_Inflate: " + (end - start) + " ms")
    for (let i = 0; i < 10; i++) {
        if (arr[i] == null) {
            // print("Inflate fail")
        }
    }
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    test();
}

ArkTools.waitAllJitCompileFinish();

test()

function test() {
    test1()
    test2()
    test3()
    test4()
    test5()
}